//
//  STPasswordViewController.swift
//  BasicAnimation-Swift
//
//  Created by Stan on 2017-02-14.
//  Copyright © 2017 stan. All rights reserved.
//

import UIKit

class STPasswordViewController: UIViewController {
    
    @IBOutlet weak var phoneNumberTextField: UITextField!
    
    
    @IBOutlet weak var pingTextField: UITextField!
    
    
    @IBOutlet weak var loginBtn: UIButton!
    
    //    电话号码文本框的中心点约束
    @IBOutlet weak var phoneNumberCenterX: NSLayoutConstraint!
    
    @IBOutlet weak var pinTextCenterX: NSLayoutConstraint!
    
    //    登录按钮中心点的约束
    @IBOutlet weak var loginBtnCenterX: NSLayoutConstraint!
    
    
    //    实图将要显示出来的方法
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //        修改电话号码、密码、登录按钮中心点的约束，为动画做准备
        phoneNumberCenterX.constant -= self.view.bounds.width
        pinTextCenterX.constant -= self.view.bounds.width
        loginBtnCenterX.constant -= self.view.bounds.width
        
        //        隐藏登录按钮
        self.loginBtn.alpha = 0
        
    }
    
    
    //    视图已经显示
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        //        将登录按钮显示出来
        self.loginBtn.alpha = 1
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        设置边框圆角
        phoneNumberTextField.layer.cornerRadius = 5
        pingTextField.layer.cornerRadius = 5
        loginBtn.layer.cornerRadius = 5
        
        
        //        设置电话号码框移入的动画
        UIView.animate(withDuration: 0.5, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.phoneNumberCenterX.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
        }, completion: nil)
        
        
        //        设置密码框移入的动画，为了有节奏，要稍微比电话号码晚一点点出现。delay设置为0.1
        UIView.animate(withDuration: 0.5, delay: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.pinTextCenterX.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
        }, completion: nil)
        
        
        //        登录按钮的动画
        UIView.animate(withDuration: 0.5, delay: 0.2, options: UIViewAnimationOptions.curveEaseOut, animations: {
            self.loginBtnCenterX.constant += self.view.bounds.width
            self.view.layoutIfNeeded()
        }, completion: nil)
        
    }
    
    
    //    设置电池栏的状态为白色
    override var preferredStatusBarStyle : UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }
    
    //    登录按钮点击事件
    @IBAction func loginBtnClick(_ sender: Any) {
        
        //        这里模拟的是登录失败的状况。设置一个阻尼动画，失败的时候登录按钮进行晃动
        UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 20, options: UIViewAnimationOptions.curveLinear, animations: {
            
            self.loginBtn.bounds = CGRect(x: self.loginBtn.bounds.origin.x - 20, y: self.loginBtn.bounds.origin.y, width: self.loginBtn.bounds.size.width, height: self.loginBtn.bounds.size.height)
            //            在动画没有播放完成之前，关闭按钮的交互
            self.loginBtn.isEnabled = false
        }) { (result) in
            
            //            动画播放完成后，重新开启按钮的交互
            self.loginBtn.isEnabled = true
            
            //            把登录按钮的位置进行复原
            self.loginBtn.bounds = CGRect(x: self.loginBtn.bounds.origin.x + 20, y: self.loginBtn.bounds.origin.y, width: self.loginBtn.bounds.size.width, height: self.loginBtn.bounds.size.height)
            
        }
    }
    
    
    //    返回按钮的点击事件
    @IBAction func backBtnClick(_ sender: Any) {
        _ =  navigationController?.popViewController(animated: true)
        
    }
    
    
}
